<template>
  <div class="app-container">

      <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
              <el-button
                      type="primary"
                      plain
                      icon="Plus"
                      @click="handleAdd"
                      v-hasPermi="['SYSConfig:productionType:add']"
              >新增</el-button>
          </el-col>
          <el-col :span="1.5">
              <el-button
                      type="success"
                      plain
                      icon="Edit"
                      :disabled="single"
                      @click="handleUpdate"
                      v-hasPermi="['SYSConfig:productionType:edit']"
              >修改</el-button>
          </el-col>
          <el-col :span="1.5">
              <el-button
                      type="danger"
                      plain
                      icon="Delete"
                      :disabled="multiple"
                      @click="handleDelete"
                      v-hasPermi="['SYSConfig:productionType:remove']"
              >删除</el-button>
          </el-col>
          <el-col :span="1.5">
              <el-button
                      type="warning"
                      plain
                      icon="Download"
                      @click="handleExport"
                      v-hasPermi="['SYSConfig:productionType:export']"
              >导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <t-ag-grid
          :style="{height: height}"
          :column-defs="column"
          :row-data="productionTypeList"
          :row-buffer="20"
          row-selection="multiple"
          :suppress-row-click-selection="true"
          :row-multi-select-with-click="true"
          align="center"
          header-align="center"
          :defaultColDef = "{
              menuTabs: ['filterMenuTab', 'columnsMenuTab']
          }"
          @selection-changed="selectionChanged"
          />

      <pagination
              v-show="total>0"
              :total="total"
              v-model:page="queryParams.pageNum"
              v-model:limit="queryParams.pageSize"
              @pagination="getList"
      />

      <!-- 添加或修改制造类型对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
          <el-form ref="productionTypeRef" :model="form" :rules="rules" label-width="80px">
                              <el-form-item label="制造类型" prop="productionTypeId">
                                  <el-input v-model="form.productionTypeId" placeholder="请输入制造类型" />
                              </el-form-item>
                              <el-form-item label="描述" prop="factoryAreaId">
                                <el-input v-model="form.productionTypeDes" placeholder="请输入描述" />
                              </el-form-item>
                              <el-form-item label="制造区域" prop="factoryAreaId">
                                  <!-- <el-input v-model="form.factoryAreaId" placeholder="请输入制造区域" /> -->
                                  <el-select v-model="form.factoryAreaId" placeholder="请输入制造区域">
                                      <el-option
                                              v-for="dict in oee_b_factory"
                                              :key="dict.value"
                                              :label="dict.label"
                                              :value="dict.value"
                                      ></el-option>
                                  </el-select>
                              </el-form-item>
          </el-form>
          <template #footer>
              <div class="dialog-footer">
                  <el-button type="primary" @click="submitForm">确 定</el-button>
                  <el-button @click="cancel">取 消</el-button>
              </div>
          </template>
      </el-dialog>
  </div>
</template>

<script setup name="ProductionType">
  import { listProductionType, getProductionType, delProductionType, addProductionType, updateProductionType } from "@/api/SYSConfig/productionType"

  const { proxy } = getCurrentInstance()
  const { oee_b_production_type, oee_b_factory_area } = proxy.useDict('oee_b_production_type', 'oee_b_factory_area')

  const productionTypeList = ref([])
  const open = ref(false)
  const loading = ref(true)
  const showSearch = ref(true)
  const ids = ref([])
  const single = ref(true)
  const multiple = ref(true)
  const total = ref(0)
  const title = ref("")

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10000
    },
    rules: {
    }
  })

  const { queryParams, form, rules } = toRefs(data)

  const height = ref('calc(100% - 65px)')

  const column = [
    { headerName: "制造类型", field: "productionTypeId", headerCheckboxSelection: true, checkboxSelection: true, minWidth: 100, flex: 1 },
    { headerName: "描述", field: "productionTypeDes", minWidth: 300, flex: 1 },
    { headerName: "制造区域", field: "factoryAreaId", minWidth: 70, flex: 1 },
    { headerName: "创建人", field: "createUser", minWidth: 70, flex: 1 },
    { headerName: "创建时间", field: "createTime", minWidth: 100, flex: 1 },
    { headerName: "更新人", field: "updateUser", minWidth: 70, flex: 1 },
    { headerName: "更新时间", field: "updateTime", minWidth: 100, flex: 1 }
  ]

  column.forEach((item) => {
    item.filter = 'agMultiColumnFilter'
  })

  const gridApi = ref()

  const selectionChanged  = (params) => {
    gridApi.value = params.api
    const selectedRows = params.api.getSelectedRows()
    single.value = selectedRows.length !== 1
    multiple.value = !selectedRows.length
  }

  /** 查询制造类型列表 */
  function getList() {
    loading.value = true
    listProductionType(queryParams.value).then(response => {
      productionTypeList.value = response.rows
      total.value = response.total
      loading.value = false
    })
  }

  // 取消按钮
  function cancel() {
    open.value = false
    reset()
  }

  // 表单重置
  function reset() {
    form.value = {
      productionTypeId: [],
      productionTypeDes: null,
      factoryAreaId: [],
      createUser: null,
      createTime: null,
      updateUser: null,
      updateTime: null
    }
    proxy.resetForm("productionTypeRef")
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef")
    handleQuery()
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset()
    open.value = true
    title.value = "添加制造类型"
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset()
    const _productionTypeId = gridApi.value.getSelectedRows()[0].productionTypeId
    getProductionType(_productionTypeId).then(response => {
      form.value = response.data
      form.value.productionTypeId = form.value.productionTypeId.split(",")
      form.value.factoryAreaId = form.value.factoryAreaId.split(",")
      open.value = true
      title.value = "修改制造类型"
    })
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["productionTypeRef"].validate(valid => {
      if (valid) {
        form.value.productionTypeId = form.value.productionTypeId.join(",")
        form.value.factoryAreaId = form.value.factoryAreaId.join(",")
        if (form.value.productionTypeId != null) {
          updateProductionType(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          })
        } else {
          addProductionType(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
        }
      }
    })
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _productionTypeIds = []
    gridApi.value.getSelectedRows().map(item => _productionTypeIds.push(item.productionTypeId) )
    proxy.$modal.confirm('是否确认删除制造类型编号为"' + _productionTypeIds + '"的数据项？').then(function() {
      return delProductionType(_productionTypeIds)
    }).then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    }).catch(() => {})
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('SYSConfig/productionType/export', {
      ...queryParams.value
    }, `productionType_${new Date().getTime()}.xlsx`)
  }

  getList()
</script>
